package com.study.transaction.order.schedule;

import com.alibaba.fastjson.JSONObject;
import com.study.transaction.order.service.MQService;
import com.study.transaction.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 兜底方案 --- 计划任务去检查，针对长时间没有发送成功的消息，然后重试。
 *
 * @author: chenhs
 * @date: Created in 11:44 2019/12/17
 **/
@Component
public class ScheduleTask {

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Autowired
    private OrderService orderService;
    @Autowired
    private MQService mqService;

    @Scheduled(fixedRate = 3000)
    public void reportCurrentTime() {
        System.out.println("当前时间：" + dateFormat.format(new Date()));

        //检查消息记录表，根据数据库 记录状态
        List<JSONObject> list = orderService.listUnDispatch();

        for (JSONObject json:list) {
            mqService.sendMsg(json);
        }

        //如果持续异常：不是系统自动能够解决的，需要监控预警，人工干预！

    }

}
